package com.sls.sys.service.impl;

import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.page.Pagination;
import com.sls.sys.dao.OperatorDAO;
import com.sls.sys.dao.RolePermissionDAo;
import com.sls.sys.pojo.OperatorInfoVO;
import com.sls.sys.pojo.PermissionInfoVO;
import com.sls.sys.pojo.RolePermission;
import com.sls.sys.service.RolePermissionService;

@Service("rolePermissionService")
public class RolePermissionServiceImpl implements RolePermissionService {
	@Resource(name = "rolePermissionDAO")
	private RolePermissionDAo rolePermissionDAo;
	@Resource(name = "operatorDAO")
	private OperatorDAO operatorDAO;
	private Gson gson = CommonUtil.gson();

	@Override
	public Pagination<RolePermission> findPage(String body, Head head) throws Exception {
		RolePermission rp = gson.fromJson(body, RolePermission.class);
		Pagination<RolePermission> pagination = rolePermissionDAo.fiandPage(rp);
		return pagination;
	}

	@Override
	public List<PermissionInfoVO> findByRoleId(String body, Head head) throws Exception {
		RolePermission rp = gson.fromJson(body, RolePermission.class);
		List<PermissionInfoVO> list = rolePermissionDAo.findPermissionByRoleId(rp
				.getRoleId());
		return list;
	}

	@Override
	public void deleteRolePermission(String body, Head head) throws Exception {
		RolePermission rp = gson.fromJson(body, RolePermission.class);
		rolePermissionDAo.deleteRolePermission(rp.getRoleId());
	}

	
	@Override
	public void updateRolePermission(String body, Head head) throws Exception {
		RolePermission rp = gson.fromJson(body, RolePermission.class);
		List<String> list = gson.fromJson(rp.getPermissionCode(),
				new TypeToken<List<String>>() {
				}.getType());
		rolePermissionDAo.deleteRolePermission(rp.getRoleId());
		OperatorInfoVO o = operatorDAO.queryAllById(head.getUserId());
		rp.setAccount(o.getAccount());
		if (list != null) {
			for (String string : list) {
				rp.setPermissionCode(string);
				rp.setUuid(UUID.randomUUID().toString());
				rolePermissionDAo.insertRolePermission(rp);
			}
		}
	}
}
